package com.zyj.test.month1.day6;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Yijia Zhang
 * @create 2024-04-27 23:13
 */
public class code0012 {
    public String intToRoman(int num) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "I");
        map.put(5, "V");
        map.put(10, "X");
        map.put(50, "L");
        map.put(100, "C");
        map.put(500, "D");
        map.put(1000, "M");
        int level = 1000;
        StringBuilder res = new StringBuilder();
        while (num > 0) {
            String one = map.get(level);
            String five = map.get(level * 5);
            String ten = map.get(level * 10);
            int time = num / level;
            if (time < 4 && time > 0) {
                for (int i = 0; i < time; i++) {
                    res.append(one);
                }
            } else if (time == 4) {
                res.append(one);
                res.append(five);
            } else if (time < 9 && time > 0) {
                res.append(five);
                for (int i = 0; i < time - 5; i++) {
                    res.append(one);
                }
            } else if (time == 9) {
                res.append(one);
                res.append(ten);
            }
            num = num % level;
            level /= 10;

        }
        return res.toString();

    }
}
